package com.android.activity.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.R;
import com.android.bean.BaseRecords;
import com.android.bean.queryBean.UserGoodsRecords;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class UserRecordsAdapter extends RecyclerView.Adapter<UserRecordsAdapter.UserRecordsAdapterHolder> {
    private Context context;
    private BaseRecords<UserGoodsRecords> recordsData;
    private Long userId;

    public UserRecordsAdapter(Context context, BaseRecords<UserGoodsRecords> recordsData,Long userId) {
        this.context = context;
        this.recordsData = recordsData;
        this.userId = userId;
    }

    @NonNull
    @Override
    public UserRecordsAdapterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view =  LayoutInflater.from(parent.getContext()).
                inflate(R.layout.recycleview_message_item, parent, false);
        return new UserRecordsAdapterHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UserRecordsAdapterHolder holder, int position) {
//        if(position <= recordsData.getTotal()&&recordsData.getTotal()<=recordsData.getSize()){
            Date date=new Date();
            date.setTime(recordsData.getRecords().get(position).getCreateTime()+ 8 * 60 * 60 * 1000);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String fmDate=simpleDateFormat.format(date);

            Log.e("1",String.valueOf(recordsData.getRecords().get(position).getCreateTime()));
            Log.e("1",fmDate);
            Log.e("1",String.valueOf(recordsData.getRecords().get(position).getSellerId()));
            Log.e("1",String.valueOf(userId));
            if (recordsData.getRecords().get(position).getSellerId() == userId){
                holder.getTvName().setText("当前用户商品被购买了："+recordsData.getRecords().get(position).getGoodsDescription()+"    "+fmDate);
            }else {
                holder.getTvName().setText("当前用户购买了: "+recordsData.getRecords().get(position).getGoodsDescription()+"    "+fmDate);
            }
//        }

    }

    @Override
    public int getItemCount() {
        if (recordsData.getTotal()<recordsData.getSize()){
            return recordsData.getTotal();
        }
        return recordsData.getSize();
    }

    public class UserRecordsAdapterHolder extends RecyclerView.ViewHolder {
        private TextView tvName;

        public UserRecordsAdapterHolder(@NonNull View itemView) {

            super(itemView);
            tvName = itemView.findViewById(R.id.tv_message_item_desc);
        }

        public TextView getTvName() {
            return tvName;
        }

        public void setTvName(TextView tvName) {
            this.tvName = tvName;
        }
    }
}
